<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv=X-UA-Compatible content="IE=edge,chrome=1">
    <meta content=always name=referrer>
    <link rel="stylesheet" type="text/css" href="__STATIC__/css/bootstrap.min.css">
    <link rel="stylesheet" type="text/css" href="__STATIC__/css/base_user.css">
    <script src="__STATIC__/js/jquery-1.8.2.js"></script>
    <title>优惠套餐</title>
    <script>
        $(function () {

            var obj = $('#total_price');
            $(".choose img").toggle(
                    function () {
                        $(this).attr("src", "__STATIC__/img_user/1234.png")
                        obj.text((parseFloat(obj.text()) - parseFloat($(this).attr('zhi'))).toFixed(2));
                    },
                    function () {
                        $(this).attr("src", "__STATIC__/img_user/123.png")
                        obj.text((parseFloat(obj.text()) + parseFloat($(this).attr('zhi'))).toFixed(2));
                    }
            )
        })
    </script>
    <style>
        .num{border-bottom: 5px solid #F5F5F5;overflow: hidden;padding: 2% 0;}
        .num span{display: block;float: left;margin-left: 5%;}
        .num ul{margin-right: 5%;float: right;overflow: hidden;border: 1px solid #B3B3B3;border-radius: 4px;width:30%;}
        .num ul li{float: left;font-size: 14px;line-height:24px;width:32%;text-align: center;}
        .num ul li:nth-of-type(2){border-left: 1px solid #B3B3B3;border-right: 1px solid #B3B3B3;}
    </style>
</head>
<body>
<div class="wrap-yh">
    <div class="header">
        <p>优惠套餐</p>
        <!--<span style="position: absolute;float: right;top: 30%;margin-left: 85%" onclick="edit()">编辑</span>-->
        <img src="__STATIC__/img_user/back.png" class="back" onclick="back_url()">
    </div>
    {if condition="$data.shopGoodsComboSelected"}
    {volist name="data.shopGoodsComboSelected" id='item'}
    <div class="show_1">
        <div class="choose" style="display: none;"><a href="#"><img src="__STATIC__/img_user/123.png" zhi="{$item.shopSpecAttr.price}"></a></div>
        <div class="tc1 show_2">
            <div class="center">
                <a href="javascript:void(0);"><img src="/{$item.shopGoods.thumb_img}" style="" onerror=this.src='/static/mobile/img/empty_img.png' ></a>
                <div>
                    <h4><a href="javascript:jump_goods_details({$item.shopGoods.id});">{$item.shopGoods.title}</a></h4>
                    <div class="size">
                        <div onclick="dialog_goods_attr({$item.shopGoods.id})"><span>属性规格：<font id="_number_{$item.shopGoods.id}" class="goods_attr" >{if condition="$item.shopSpecAttr.attribute"}{$item.shopSpecAttr.attribute}{else/}无属性{/if}</font> </span><img src="__STATIC__/img_user/down2.png">
                        </div>
                        <input type="hidden" id="goods_attr_{$item.shopGoods.id}" class='attr_id' value="{$item.goods_attr_id}" />
                        <span>×1</span>
                    </div>
                    <div class="price">
                        <span>&yen;<font id="old_price">{if condition="$item.shopSpecAttr.price"}{$item.shopSpecAttr.price}{else/}{$item.shopGoods.price}{/if}</font></span>
                        <!--<div class="qianbi">-->
                            <!--<span>佣金</span>-->
                            <!--<img src="__STATIC__/img_user/sy-qianbi.png">-->
                            <!--<font class="scale">%{$item.shopGoods.rebate_percentage}</font>-->
                        <!--</div>-->
                    </div>
                </div>
            </div>
        </div>
    </div>
    {/volist}
    {/if}
    <!--属性选择开始-->
    <div class="spsx" style="bottom:10px;"></div>
    <!--属性选择结束-->
    <div style="width: 100%;height: 110px;"></div>

    <div class="footer">
        <!--数量选择开始-->
        <div class="num" style="border-bottom:1px solid #F5F5F5;">
            <span>购买数量</span>
            <ul>
                <li onclick="jian()">-</li>
                <li><input type="text" id="num" name="num" onkeyup="num_control(this,1)" value="1" style="width: 100%;outline: none;border: 0;text-align: center" /></li>
                <li onclick="jia()">+</li>
            </ul>
        </div>
        <!--数量选择结束-->
        <div class="total">合计：<span>&yen;<font id="total_price">{$data.total}</font> <span style="font-size: 12px;transform: scale(0.9);white-space: pre;color: #C1C1C1;">{if condition="$freight"}(运费:{$freight}元){else/}(不含运费){/if}</span></span></div>
        <div class="buy" style="" >优惠价：&yen;<font id="special">{$data.combo_price}</font> <span onclick="buy_now()">  立即购买  </span></div>
        <input type="hidden" id="total" value="{$data.total}"/>
    </div>
    <div class="board" onclick="$('.board').height('');$('.spsx').css({display: ''});" ></div>

</div>
<script src="__STATIC__/js/layer_mobile/layer.js"></script>
<script src="__STATIC__/js/combo.js"></script>
<script>
    //返回商品详情
    function back_url(){
        var url = sessionStorage.getItem('combo_url');
        if (!url){
            window.location.href = "{:url('mobile/user_port/index')}";
        }else{
            window.location.href = url;
        }
    }
    function dialog_goods_attr(goods_id){
        var url = "{:url('mobile/user_cart/combo_attr')}";
        attr_dialog(goods_id, url);
        tc();
    }
    function tc() {
        var h = window.screen.height;
        $(".board").height(h + "px");
        $(".spsx").css({display: "block"});
    }
    //编辑
    function edit() {
        $('.show_1').each(function (i, o) {
            $(o).addClass('tc')
        })
        $('.choose').toggle();
        $('#special').toggle();
        var total = 0.00;
        $('.old_price').each(function (i, o) {
            total += parseInt($(o).text());
        })
        if ($('.choose').css('display') == 'none') {
            $('#total_price').text(parseInt($('#total').val()).toFixed(2));
        } else {
            $('#total_price').text(parseInt(total).toFixed(2));
        }
    }
    //立即购买
    function buy_now(){
        //检测套餐是否符合条件
        $.post("{:url('mobile/user_category/check_combo')}",{'combo_id':"{:input('combo_id')}"},function (data) {
            if (data.code == 200){
                var num = $('#num').val();
                sessionStorage.setItem('goods_order_yes_url',window.location.href);
                //统计商品属性
                var combo_attr = '';   //无属性为0
                $('.attr_id').each(function (i,o) {
                    if (!combo_attr){
                        combo_attr = $(o).val();
                    }else{
                        combo_attr += ','+$(o).val();
                    }
                });
                var param = "{:input('combo_id')}"+'|'+combo_attr+'|'+num;
                window.location.href = "{:url('mobile/user_category/order_yes')}?combo="+param;
            }else{
                layer.open({content:data.message,skin:'msg',time:2});
            }
        })
    }
    //跳转到商品详情
    function jump_goods_details(id){
        sessionStorage.setItem('goods_url_back',window.location.href);
        window.location.href = "{:url('mobile/user_category/goods_details')}?goods_id="+id;
    }
    //数量控制
    function num_control(o,n){

        if(!/^\d+$/.test(o.value)) {
            o.value=o.value.replace(/[^\d]+/g,'');
        }
        if (o.value == ''){
            setTimeout(function () {
                if (o.value == '' || o.value === 0){
                    o.value = n;
                }
            },1000);
        }else if(o.value >= 99){
            o.value = 99
        }else if(o.value < 1){
            o.value = 1;
            layer.open({content:'数量至少为 1',skin:'msg',time:2})
        }
        change_text();
    }

    function jian(){
        var num = $('#num');
        if (num.val() == 1){
            layer.open({content:'不能再减了',skin:'msg',time:2});
            return false;
        }
        num.val(num.val()-1);
        change_text();
    }
    function jia(){
        var num = $('#num');
        num.val(parseInt(num.val())+1);
        change_text();
    }
    //改变价格
    function change_text(){
        var num = $('#num').val();
        if (num != ''){
            var new_price = changeTwoDecimal_f(accAdd(accMul(Subtr("{$data.total}","{$freight}"),num),"{$freight}"));
            $('#total_price').text(new_price);
            $('#special').text(changeTwoDecimal_f(accMul("{$data.combo_price}",num)));
        }
    }
    //乘法函数
    function accMul(arg1,arg2)
    {
        var m=0,s1=arg1.toString(),s2=arg2.toString();
        try{m+=s1.split(".")[1].length}catch(e){}
        try{m+=s2.split(".")[1].length}catch(e){}
        return Number(s1.replace(".",""))*Number(s2.replace(".",""))/Math.pow(10,m)
    }
    //加法函数
    function accAdd(arg1,arg2){
        var r1,r2,m;
        try{r1=arg1.toString().split(".")[1].length}catch(e){r1=0}
        try{r2=arg2.toString().split(".")[1].length}catch(e){r2=0}
        m=Math.pow(10,Math.max(r1,r2))
        return (arg1*m+arg2*m)/m
    }
    //减法函数
    function Subtr(arg1,arg2){
        var r1,r2,m,n;
        try{r1=arg1.toString().split(".")[1].length}catch(e){r1=0}
        try{r2=arg2.toString().split(".")[1].length}catch(e){r2=0}
        m=Math.pow(10,Math.max(r1,r2));
        //last modify by deeka
        //动态控制精度长度
        n=(r1>=r2)?r1:r2;
        return ((arg1*m-arg2*m)/m).toFixed(n);
    }
    //强制保留两位小数
    function changeTwoDecimal_f(x) {
        var f_x = parseFloat(x);
        if (isNaN(f_x)) {
            alert('function:changeTwoDecimal->parameter error');
            return false;
        }
        var f_x = Math.round(x * 100) / 100;
        var s_x = f_x.toString();
        var pos_decimal = s_x.indexOf('.');
        if (pos_decimal < 0) {
            pos_decimal = s_x.length;
            s_x += '.';
        }
        while (s_x.length <= pos_decimal + 2) {
            s_x += '0';
        }
        return s_x;
    }
</script>
</body>
</html>